package com.trs.template.dao.greendao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.trs.template.dao.greendao.Beacon;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "BEACON".
*/
public class BeaconDao extends AbstractDao<Beacon, Long> {

    public static final String TABLENAME = "BEACON";

    /**
     * Properties of entity Beacon.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Uuid = new Property(1, String.class, "uuid", false, "UUID");
        public final static Property Major = new Property(2, int.class, "major", false, "MAJOR");
        public final static Property Minor = new Property(3, int.class, "minor", false, "MINOR");
        public final static Property Time = new Property(4, java.util.Date.class, "time", false, "TIME");
        public final static Property Temperature = new Property(5, Double.class, "temperature", false, "TEMPERATURE");
        public final static Property Light = new Property(6, Double.class, "light", false, "LIGHT");
        public final static Property Lon = new Property(7, Double.class, "lon", false, "LON");
        public final static Property Lat = new Property(8, Double.class, "lat", false, "LAT");
        public final static Property User_id = new Property(9, Long.class, "user_id", false, "USER_ID");
    };

    private DaoSession daoSession;

    private Query<Beacon> user_BeaconListQuery;

    public BeaconDao(DaoConfig config) {
        super(config);
    }
    
    public BeaconDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"BEACON\" (" + //
                "\"_id\" INTEGER PRIMARY KEY AUTOINCREMENT ," + // 0: id
                "\"UUID\" TEXT NOT NULL ," + // 1: uuid
                "\"MAJOR\" INTEGER NOT NULL ," + // 2: major
                "\"MINOR\" INTEGER NOT NULL ," + // 3: minor
                "\"TIME\" INTEGER," + // 4: time
                "\"TEMPERATURE\" REAL," + // 5: temperature
                "\"LIGHT\" REAL," + // 6: light
                "\"LON\" REAL," + // 7: lon
                "\"LAT\" REAL," + // 8: lat
                "\"USER_ID\" INTEGER);"); // 9: user_id
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"BEACON\"";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, Beacon entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
        stmt.bindString(2, entity.getUuid());
        stmt.bindLong(3, entity.getMajor());
        stmt.bindLong(4, entity.getMinor());
 
        java.util.Date time = entity.getTime();
        if (time != null) {
            stmt.bindLong(5, time.getTime());
        }
 
        Double temperature = entity.getTemperature();
        if (temperature != null) {
            stmt.bindDouble(6, temperature);
        }
 
        Double light = entity.getLight();
        if (light != null) {
            stmt.bindDouble(7, light);
        }
 
        Double lon = entity.getLon();
        if (lon != null) {
            stmt.bindDouble(8, lon);
        }
 
        Double lat = entity.getLat();
        if (lat != null) {
            stmt.bindDouble(9, lat);
        }
 
        Long user_id = entity.getUser_id();
        if (user_id != null) {
            stmt.bindLong(10, user_id);
        }
    }

    @Override
    protected void attachEntity(Beacon entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public Beacon readEntity(Cursor cursor, int offset) {
        Beacon entity = new Beacon( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.getString(offset + 1), // uuid
            cursor.getInt(offset + 2), // major
            cursor.getInt(offset + 3), // minor
            cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)), // time
            cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5), // temperature
            cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6), // light
            cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7), // lon
            cursor.isNull(offset + 8) ? null : cursor.getDouble(offset + 8), // lat
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9) // user_id
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, Beacon entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setUuid(cursor.getString(offset + 1));
        entity.setMajor(cursor.getInt(offset + 2));
        entity.setMinor(cursor.getInt(offset + 3));
        entity.setTime(cursor.isNull(offset + 4) ? null : new java.util.Date(cursor.getLong(offset + 4)));
        entity.setTemperature(cursor.isNull(offset + 5) ? null : cursor.getDouble(offset + 5));
        entity.setLight(cursor.isNull(offset + 6) ? null : cursor.getDouble(offset + 6));
        entity.setLon(cursor.isNull(offset + 7) ? null : cursor.getDouble(offset + 7));
        entity.setLat(cursor.isNull(offset + 8) ? null : cursor.getDouble(offset + 8));
        entity.setUser_id(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(Beacon entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(Beacon entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "beaconList" to-many relationship of User. */
    public List<Beacon> _queryUser_BeaconList(Long user_id) {
        synchronized (this) {
            if (user_BeaconListQuery == null) {
                QueryBuilder<Beacon> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.User_id.eq(null));
                user_BeaconListQuery = queryBuilder.build();
            }
        }
        Query<Beacon> query = user_BeaconListQuery.forCurrentThread();
        query.setParameter(0, user_id);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getUserDao().getAllColumns());
            builder.append(" FROM BEACON T");
            builder.append(" LEFT JOIN USER T0 ON T.\"USER_ID\"=T0.\"_id\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Beacon loadCurrentDeep(Cursor cursor, boolean lock) {
        Beacon entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        User user = loadCurrentOther(daoSession.getUserDao(), cursor, offset);
        entity.setUser(user);

        return entity;    
    }

    public Beacon loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Beacon> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Beacon> list = new ArrayList<Beacon>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Beacon> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Beacon> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
